const fs = require('fs');
const path = require('path');
require('dotenv').config();

function ensureDirectoryExists(targetPath) {
    if (!targetPath) return;
    try {
        fs.mkdirSync(targetPath, { recursive: true });
    } catch (_err) {
        // best-effort; failures will surface later where used
    }
}

const BASE_DIR = process.env.BASE_DIR || path.resolve(process.cwd(), 'downloads');
const DB_PATH = process.env.DB_PATH || path.resolve(process.cwd(), 'data', 'downloads.db');

// Ensure base directories exist
ensureDirectoryExists(BASE_DIR);
ensureDirectoryExists(path.dirname(DB_PATH));

module.exports = {
    BASE_DIR,
    DB_PATH,
    PORT: process.env.PORT || 3000,
    DEFAULT_TIMEOUT_MS: Number(process.env.DEFAULT_TIMEOUT_MS || 30000),
    DEFAULT_RETRY: Number(process.env.DEFAULT_RETRY || 3),
    CONCURRENCY: Number(process.env.CONCURRENCY || 10),
    ALLOWED_DOMAINS: (process.env.ALLOWED_DOMAINS || '')
        .split(',')
        .map((s) => s.trim())
        .filter(Boolean),
    LOG_LEVEL: process.env.LOG_LEVEL || 'info',
};


